package com.message.api.group.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.base.helper.StringUtils;
import com.message.api.group.dao.AcModuleMapper;
import com.message.api.group.entity.AcModule;
import com.message.api.group.service.AcModuleService;
import com.message.api.group.vo.AcModuleVo;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mzl
 * @since 2020-07-09
 */
@Service
public class AcModuleServiceImpl extends ServiceImpl<AcModuleMapper, AcModule> implements AcModuleService {


    @Override
    public List<AcModuleVo> findByExample(AcModuleVo example) {
        return baseMapper.findByExample(buildWrapper(example));
    }

    @Override
    public Page<AcModuleVo> findByPage(AcModuleVo example, Page page) {
        page.setOptimizeCountSql(false);
        return page.setRecords(baseMapper.findByExample(buildWrapper(example), page));
    }

    @Override
    public List<AcModule> findByGrouptypeid(Integer grouptypeid) {
        QueryWrapper<AcModule> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(AcModule::getGrouptypeid, grouptypeid)
                .eq(AcModule::getState, "1")
                .orderByAsc(AcModule::getModulenumber);
        return this.list(wrapper);
    }

    private QueryWrapper<AcModuleVo> buildWrapper(AcModuleVo example) {
        QueryWrapper<AcModuleVo> wrapper = new QueryWrapper<>();

        /** 条件查询 */
        String  name          = example.getName();
        Integer grouptypeid   = example.getGrouptypeid();
        String  grouptypeName = example.getGrouptypeName();
        wrapper.like(StringUtils.isNotEmpty(grouptypeName), "grouptype_name", grouptypeName);
        wrapper.lambda()
                .like(StringUtils.isNotEmpty(name), AcModuleVo::getName, name)
                .eq(null != grouptypeid, AcModuleVo::getGrouptypeid, grouptypeid)
                .eq(AcModuleVo::getState, "1")
                .orderByAsc(AcModuleVo::getId);
        return wrapper;
    }

}
